package com.yidong;

import com.yidong.entity.YiDongObj;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

import java.io.*;

/**
 * 开票
 */
public class KaiPiao {

    public static String kpsqb_path = "D:\\code\\Projects\\yidong\\src\\main\\java\\com\\yidongcfg\\开票模板\\开票申请表.xlsx";
    public static String kpsm_path = "D:\\code\\Projects\\yidong\\src\\main\\java\\com\\yidongcfg\\开票模板\\开票说明.docx";
    public static String kpxxqrb_path = "D:\\code\\Projects\\yidong\\src\\main\\java\\com\\yidongcfg\\开票模板\\开票信息确认表.xlsx";


    public static String kpsqb_out = "D:\\code\\Projects\\yidong\\src\\main\\java\\com\\yidongcfg\\开票申请表.xlsx";
    public static String kpsm_out = "D:\\code\\Projects\\yidong\\src\\main\\java\\com\\yidongcfg\\开票说明.docx";
    public static String kpxxqrb_out = "D:\\code\\Projects\\yidong\\src\\main\\java\\com\\yidongcfg\\开票信息确认表.xlsx";

    //开票方法汇总
    public void kaiPiao(YiDongObj obj){
        kaiPiaoShenQing(obj);
        kaiPiaoShuoMing(obj);
        xinXiQueRen(obj);
    }


    /**
     * 开票申请表 xlsx
     * @param obj
     */
    public void kaiPiaoShenQing(YiDongObj obj){
        try (FileInputStream fis = new FileInputStream(new File(kpsqb_path));
             Workbook workbook = new XSSFWorkbook(fis)) {

            Sheet sheet = workbook.getSheetAt(0); // 获取第一个Sheet，索引从0开始

            for (Row row : sheet) {
                for (Cell cell : row) {
                    // 只在文本类型的单元格中查找和替换
                    if (cell.getCellType() == CellType.STRING) {
                        String originalText = cell.getStringCellValue();
                        String runText = originalText;

                        //替换
                        runText = runText.replaceAll("%项目名称%",obj.getXmmc());
                        runText = runText.replaceAll("%合同编号%",obj.getHtbhkp());
                        runText = runText.replaceAll("%单位名称%",obj.getDwmc());
                        runText = runText.replaceAll("%开票内容%",obj.getKpnr());
                        runText = runText.replaceAll("%详细信息%",obj.getXxxx());
                        runText = runText.replaceAll("%开票金额%",obj.getKpje());
                        runText = runText.replaceAll("%发票类型%",obj.getFplx().equals("普通")?"普":"专");

                        // 如果替换后的文本与原始文本不同，则更新单元格
                        if (!originalText.equals(runText)) {
                            cell.setCellValue(runText);
                        }
                    }
                }
            }

            // 将修改后的Workbook写入到新文件
            try (FileOutputStream fos = new FileOutputStream(kpsqb_out)) {
                workbook.write(fos);
                System.out.println("Excel文件已成功保存为: " + kpsqb_out);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 开票说明 docx
     * @param obj
     */
    public void kaiPiaoShuoMing(YiDongObj obj){
        try (InputStream is = new FileInputStream(kpsm_path);
             XWPFDocument document = new XWPFDocument(is)) {
            // 遍历文档中的所有段落
            for (XWPFParagraph paragraph : document.getParagraphs()) {
                for (XWPFRun run : paragraph.getRuns()) {
                    String runText = run.text();

                    //替换
                    runText = runText.replaceAll("%合同编号%",obj.getHtbhkp());
                    runText = runText.replaceAll("%合同名称%",obj.getHtmckp());
                    runText = runText.replaceAll("%收款条件%",obj.getSktj());
                    runText = runText.replaceAll("%开票金额%",obj.getKpje());
                    runText = runText.replaceAll("%发票类型%",obj.getFplx());
                    runText = runText.replaceAll("%详细信息%",obj.getXxxx());
                    runText = runText.replaceAll("%本月最后一天%",obj.getMonth_last_day());
                    runText = runText.replaceAll("%日期%",obj.getToday());

                    run.setText(runText, 0);
                }
            }

            // 保存文档到新文件
            try (OutputStream out = new FileOutputStream(kpsm_out)) {
                document.write(out);
            }
            System.out.println("文档已更新并保存为: " + kpsm_out);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 开票信息确认表 xlsx
     * @param obj
     */
    public void xinXiQueRen(YiDongObj obj){
        try (FileInputStream fis = new FileInputStream(new File(kpxxqrb_path));
             Workbook workbook = new XSSFWorkbook(fis)) {

            Sheet sheet = workbook.getSheetAt(0); // 获取第一个Sheet，索引从0开始

            for (Row row : sheet) {
                for (Cell cell : row) {
                    // 只在文本类型的单元格中查找和替换
                    if (cell.getCellType() == CellType.STRING) {
                        String originalText = cell.getStringCellValue();
                        String runText = originalText;

                        //替换
                        runText = runText.replaceAll("%项目名称%",obj.getXmmc());
                        runText = runText.replaceAll("%开票内容%",obj.getKpnr());

                        // 如果替换后的文本与原始文本不同，则更新单元格
                        if (!originalText.equals(runText)) {
                            cell.setCellValue(runText);
                        }
                    }
                }
            }

            //单独判断 开票类型 勾选
            //普票
            Row row3 = sheet.getRow(3);
            Cell cell_p = row3.getCell(4,Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
            //专票
            Row row4 = sheet.getRow(4);
            Cell cell_Z = row4.getCell(4,Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);

            if (obj.getFplx().equals("普通")) {
                cell_Z.setCellValue("");
            } else {
                cell_p.setCellValue("");
            }

            // 将修改后的Workbook写入到新文件
            try (FileOutputStream fos = new FileOutputStream(kpxxqrb_out)) {
                workbook.write(fos);
                System.out.println("Excel文件已成功保存为: " + kpxxqrb_out);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    /**
     * 退票说明 doxc
     * @param obj
     */
    public void tuiPiaoShuoMing(YiDongObj obj){

    }
}
